package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.bundle.ElementName;
import ohos.bundle.IBundleManager;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;

import java.io.File;
import java.util.List;

public class CameraIntents {

    private Context context;
    private Intent intent;

    private CameraIntents(Context context)
    {
        this.context = context;
    }

    public static CameraIntents from(Context context)
    {
        return new CameraIntents(context);
    }

    public CameraIntents openPhotoCamera()
    {
        intent = new Intent();
        intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
        return this;
    }

    public CameraIntents openVideoCamera()
    {
        intent = new Intent();
        intent.setAction("android.media.action.VIDEO_CAMERA");
        return this;
    }

    public CameraIntents capturePhoto(Uri location, String fileName)
    {
        intent = new Intent();
        intent.setAction("android.media.action.IMAGE_CAPTURE");
        intent.setParam("output", Uri.appendEncodedPathToUri(location, fileName));
        return this;
    }

    // TODO:
    private CameraIntents capturePhoto(String file)
    {
        Uri uri = Uri.getUriFromFile(new File(file));
        intent = new Intent();
        intent.setAction("android.media.action.IMAGE_CAPTURE");
        intent.setParam("output", uri);
        return this;
    }

    public CameraIntents captureVideo(Uri location, String fileName)
    {
        intent = new Intent();
        intent.setAction("android.media.action.VIDEO_CAPTURE");
        intent.setParam("output", Uri.appendEncodedPathToUri(location, fileName));
        return this;
    }

    // TODO:
    private boolean isCropAvailable() throws RemoteException {
        intent = new Intent();
        intent.setAction("com.android.camera.action.CROP");
        intent.setType("image/*");
        return isIntentAvailable(intent);
    }

    // TODO:
    private CameraIntents cropImage(File image, int outputX, int outputY, int aspectX, int aspectY, boolean scale) throws RemoteException {
        intent = new Intent();
        intent.setAction("com.android.camera.action.CROP");
        intent.setType("image/*");

        List<AbilityInfo> list = context.getBundleManager().queryAbilityByIntent(intent, 0,0);
        AbilityInfo res = list.get(0);

        intent.setParam("outputX", outputX);
        intent.setParam("outputY", outputY);
        intent.setParam("aspectX", aspectX);
        intent.setParam("aspectY", aspectY);
        intent.setParam("scale", scale);
        intent.setParam("return-data", true);
        intent.setUri(Uri.getUriFromFile(image));

        //intent.setElement(new ElementName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setElement(new ElementName("this",res.bundleName,res.getApplicationInfo().getName()));
        return this;
    }

    private boolean isIntentAvailable(Intent intent) throws RemoteException {
        IBundleManager bundleManager = context.getBundleManager();
        List<AbilityInfo> list = bundleManager.queryAbilityByIntent(intent, 65536,0);
        return list.size() > 0;
    }

    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
